﻿using System;
using System.Linq.Expressions;
using System.Runtime.Serialization;

namespace Emico.Linq.Criterion
{
    [Serializable]
    public class BetweenExpression<T> : AbstractExpression<T>
        where T : class
    {
        #region Properties

        public string PropertyName
        {
            get;
            private set;
        }

        public object LO
        {
            get;
            private set;
        }

        public object HI
        {
            get;
            private set;
        }

        #endregion

        #region Constructors

        public BetweenExpression(string propertyName, object lo, object hi)
        {
            if (string.IsNullOrWhiteSpace(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            if (lo == null)
            {
                throw new ArgumentNullException("lo");
            }

            if (hi == null)
            {
                throw new ArgumentNullException("hi");
            }

            this.PropertyName = propertyName;
            this.LO = lo;
            this.HI = hi;
        }

        protected BetweenExpression(SerializationInfo info, StreamingContext context)
        {
        }

        #endregion

        #region Implement Abstract Method

        public override Expression ToLinqExpression(Criteria<T> criteria, ParameterExpression parameter)
        {
            Type t = typeof(T);

            IExpression<T> ge = new GeExpression<T>(this.PropertyName, this.LO);
            IExpression<T> le = new LeExpression<T>(this.PropertyName, this.HI);
            Expression filter = new AndExpression<T>(ge, le).ToLinqExpression(criteria, parameter);

            return filter;
        }
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
        }

        #endregion
    }
}
